package resources

import (
	"TCP/resources/db"
	"xlib/log"

	D "TCP/define"
)

var (
	dac *Dac
)

func Init() error {
	log.Info("resources init.")
	d, err := NewDac()
	if err != nil {
		log.Error(err)
		return err
	}
	dac = d

	return nil
}

func Stop() {
	dac.stop()
}

//GetDrvInfo 获取设备信息数据
func GetDrvInfo(sn string) (int, error, D.DriverInfo) {
	return dac.GetDrvInfo(sn)
}

//GetCSQRecord 获取信号强度历史数据
func GetCSQRecord(sn string) (int, error, D.CSQData) {
	return dac.GetCSQRecord(sn)
}

//SaveDrvInfo 保存设备信息数据
func SaveDrvInfo(info db.DrvInfo) (int, error) {
	if D.DBStorage != "true" {
		return D.EcUnknown, nil
	}
	return dac.SaveDrvInfo(info)
}

//SaveDrvEvent 保存设备信息事件
func SaveDrvEvent(events []db.DrvEvent) (int, error) {
	if D.DBStorage != "true" {
		return D.EcUnknown, nil
	}
	return dac.SaveDrvEvent(events)
}

//GetDoors 获取门信息
func GetDoors() (int, error, []D.DoorInfo) {
	return dac.GetDoors()
}

//GetDoorState 获取门状态
func GetDoorState() (int, error, map[string]int) {
	return dac.GetDoorState()
}

//SetDoorState 远程控制
func SetDoorState(doorState map[string]int, ip string) (int, error) {
	return dac.SetDoorState(doorState, ip)
}

//GetTimeGroup 获取时段信息
func GetTimeGroup(groupNo int) (int, error, D.TimeInfo) {
	return dac.GetTimeGroup(groupNo)
}

//SetTimeGroup 设置时段信息
func SetTimeGroup(timeInfo D.TimeInfo) (int, error) {
	return dac.SetTimeGroup(timeInfo)
}

//ClearTimeGroup 清空时段信息
func ClearTimeGroup(groupNo int) (int, error) {
	return dac.ClearTimeGroup(groupNo)
}

//GetCards 获取所有卡信息
func GetCards() (int, error, []D.CardInfo) {
	return dac.GetCards()
}

//GetCard 获取卡信息
func GetCard(cardNo string) (int, error, D.CardInfo) {
	return dac.GetCard(cardNo)
}

//AddCard 新增卡信息
func AddCard(cardInfo D.CardInfo) (int, error) {
	return dac.AddCard(cardInfo)
}

//SetCard 设置卡信息
func SetCard(cardInfo D.CardInfo) (int, error) {
	return dac.SetCard(cardInfo)
}

//DeleteCard 删除卡信息
func DeleteCard(cardNo string) (int, error) {
	return dac.DeleteCard(cardNo)
}

//GetEvent 获取记录
func GetEvent(recordIndex, alarmIndex int) (int, error, *D.EventInfo) {
	return dac.GetEvent(recordIndex, alarmIndex)
}

//GetHistoryEvent 获取历史记录
func GetHistoryEvent(timeBegin, timeEnd string, door []int) (int, error, *D.EventHty) {
	return dac.GetHistoryEvent(timeBegin, timeEnd, door)
}

func SetCurrentTime() error {
	return dac.SyncTime()
}

func Reset() error {
	return dac.Reset()
}
